Skip to content

Optimize DialogueFeignClient small response reader #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

schlosna
Copy link
Contributor

@schlosna schlosna commented May 12, 2025

Before this PR

Small responses <= 8KiB would always allocate 8KiB ByteBuffer as InputStreamReader creates a StreamDecoder that allocates a fixed 8192 byte ByteBuffer. This allocation becomes a scalability bottleneck for high throughput RPCs with small responses (think something returning timestamps, locks, authorization results, etc.)

See https://github.com/openjdk/jdk/blob/4c03e5938df0a9cb10c2379af81163795dd3a086/src/java.base/share/classes/sun/nio/cs/StreamDecoder.java#L248

After this PR

==COMMIT_MSG==
Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, see FasterXML/jackson-core#1081 and FasterXML/jackson-benchmarks#9 (comment) for benchmarks showing between 2x and 10x speedup handling deserialization of small values.

==COMMIT_MSG==

Possible downsides?

@changelog-app
Copy link

changelog-app bot commented May 12, 2025

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Optimize DialogueFeignClient small response reader

Check the box to generate changelog(s)

  • Generate changelog entry

Copy link

stale bot commented Jun 27, 2025

This PR has been automatically marked as stale because it has not been touched in the last 14 days. If you'd like to keep it open, please leave a comment or add the 'long-lived' label, otherwise it'll be closed in 7 days.

@stale stale bot added the stale label Jun 27, 2025
@stale stale bot removed the stale label Aug 12, 2025
@changelog-app
Copy link

changelog-app bot commented Aug 12, 2025

Successfully generated changelog entry!

What happened?

Your changelog entries have been stored in the database as part of our migration to ChangelogV3.

Need to regenerate?

Simply interact with the changelog bot comment again to regenerate these entries.

Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
see FasterXML/jackson-core#1081
@schlosna schlosna force-pushed the davids/small-feign branch from e336971 to 2d19331 Compare August 12, 2025 16:57
@schlosna schlosna requested a review from Copilot August 12, 2025 16:59
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the DialogueFeignClient by avoiding InputStreamReader and HeapByteBuffer overhead for small responses (less than 8KiB). The optimization reads the entire response into memory as a string for small payloads, which can be more efficient than stream-based reading for small data.

Key changes:

  • Added optimization for small response bodies in the asReader() method
  • Updated URL decoding to use StandardCharsets.UTF_8 directly instead of string literal
  • Removed unused UnsupportedEncodingException import

if (maybeLength != null && maybeLength < 8192) {
// Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
// see https://github.com/FasterXML/jackson-core/pull/1081
try (InputStream inputStream = asInputStream()) {
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try-with-resources block closes the InputStream after creating the StringReader, but the StringReader contains the full content and doesn't need the stream to remain open. However, this creates a potential issue if asInputStream() returns the same underlying stream instance that might be needed elsewhere. Consider ensuring this optimization only applies when it's safe to consume the entire stream.

Suggested change
try (InputStream inputStream = asInputStream()) {
InputStream inputStream = asInputStream();
try {

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the response is 8KiB or less we want to fully consume the response input stream and release the response after converting to an immutable String & StringReader for consumption by Jackson deserialization

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a guarantee that if asReader() has been called, nothing will then call asInputStream()? I imagine it doesn't make much sense to call both, but I'm not very familiar with this API.

@schlosna schlosna requested review from a team and tpetracca and removed request for a team August 14, 2025 20:35
// Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
// see https://github.com/FasterXML/jackson-core/pull/1081
try (InputStream inputStream = asInputStream()) {
return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to allocate the string more directly? inputStream.readAllBytes() will allocate a byte[] and new String is going to copy that byte array into a new one, leading to twice the allocation. I'm not clear whether that is in fact more efficient than the current behavior if the length is more than 4096

if (maybeLength != null && maybeLength < 8192) {
// Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
// see https://github.com/FasterXML/jackson-core/pull/1081
try (InputStream inputStream = asInputStream()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a guarantee that if asReader() has been called, nothing will then call asInputStream()? I imagine it doesn't make much sense to call both, but I'm not very familiar with this API.

// Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
// see https://github.com/FasterXML/jackson-core/pull/1081
try (InputStream inputStream = asInputStream()) {
return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other question: Should we guard against malicious/bad responses that return an incorrect length? (e.g. I imagine there is a corner case where the length is set to 0, which doesn't make sense)
Here we'll buffer the entire response in memory. If the content length header lied to us, we might buffer a lot more than we expected, since we just read the entire thing in memory. Should we just read the first length bytes only?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants